package com.onejson.ojmall.service;

import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onejson.ojmall.common.domain.PageResult;
import com.onejson.ojmall.common.domain.ResponseDTO;
import com.onejson.ojmall.common.util.OjPageUtil;
import com.onejson.ojmall.dao.SMenuDao;
import com.onejson.ojmall.model.entity.SMenuEntity;
import com.onejson.ojmall.model.form.SMenuBatchForm;
import com.onejson.ojmall.model.form.SMenuForm;
import com.onejson.ojmall.model.form.SMenuQueryForm;
import com.onejson.ojmall.model.vo.SMenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 菜单表 Service 层
 *
 * @author wxx
 */
@Service
@Slf4j
public class SMenuService extends ServiceImpl<SMenuDao, SMenuEntity> {

    @Resource
    private SMenuDao sMenuDao;


    /**
     * 获取菜单表列表
     */
    public PageResult<SMenuVO> getList(SMenuQueryForm queryForm) {
        Page<?> page = OjPageUtil.convert2PageQuery(queryForm);
        List<SMenuVO> list = sMenuDao.query(page, queryForm);
        return OjPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询菜单表
     */
    public SMenuVO queryById(String detailId) {

        SMenuEntity detailEntity = sMenuDao.selectById(detailId);
        SMenuVO sMenuVO = new SMenuVO();

        try {
            BeanUtils.copyProperties(sMenuVO, detailEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sMenuVO;

    }


    /**
     * 【菜单表】 - 添加
     */
    public ResponseDTO<String> addSMenu(SMenuForm addForm) {

        SMenuEntity entity = new SMenuEntity();

//        addForm.setId(null);

        try {
            BeanUtils.copyProperties(entity, addForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        sMenuDao.insert(entity);
        return ResponseDTO.ok();
    }

    /**
     * 【菜单表】 - 更新
     */
    public ResponseDTO<String> updateSMenu(SMenuForm updateForm) {

        SMenuEntity entity = new SMenuEntity();

        try {
            BeanUtils.copyProperties(entity, updateForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        sMenuDao.updateById(entity);
        return ResponseDTO.ok();
    }


    /**
     * 【菜单表】- 批量更新
     */
    public ResponseDTO<String> batchUpdateSMenu(SMenuBatchForm sMenuBatchForm) {

        SMenuEntity entity = new SMenuEntity();
        List<SMenuEntity> sMenuEntityList = new ArrayList<>();

        try {
            for (SMenuForm sMenuForm : sMenuBatchForm.getSMenuFormList()) {

                sMenuForm.setCreateTime(null);
                sMenuForm.setUpdateTime(null);
//                entity.setRemark(sMenuBatchForm.getRemark());
                BeanUtils.copyProperties(entity, sMenuForm);
                sMenuEntityList.add(entity);
            }
        } catch (Exception e) {
            System.out.println("错误消息");
            System.out.println(e.getMessage());
        }
        this.updateBatchById(sMenuEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        sMenuDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        sMenuDao.deleteById(id);
        return ResponseDTO.ok();
    }


}

